#include "Grammar.h"
#include "ParsedFileListParser.h"


void Dictionary::ParsedFileListParser::startElement(const XMLCh* const uri,const XMLCh* const localname,const XMLCh* const qname,const XERCES_CPP_NAMESPACE_QUALIFIER Attributes& attrs)
{
	XERCES_CPP_NAMESPACE_USE
    char* element =  XMLString::transcode(localname);
		if (Dictionary::Grammar::FILE() == element) is_file_element = true;
		else is_file_element = false;
    XMLString::release(&element);
}

void Dictionary::ParsedFileListParser::endElement(const XMLCh *const uri,const XMLCh *const localname,const XMLCh *const qname)
{
}

void Dictionary::ParsedFileListParser::fatalError(const XERCES_CPP_NAMESPACE_QUALIFIER SAXParseException& exception)
{
	  XERCES_CPP_NAMESPACE_USE
    char* message = XMLString::transcode(exception.getMessage());
		std::cout << "Fatal Error: " << message << " at line: " << exception.getLineNumber() << std::endl;
		XMLString::release(&message);
}

void Dictionary::ParsedFileListParser::characters(const XMLCh *const chars,const unsigned int	length)
{
	if (is_file_element == false) return;
	XERCES_CPP_NAMESPACE_USE
	
  char* data = XMLString::transcode(chars);
	this->add_file(data);

	is_file_element = false;

	XMLString::release(&data);
}

Dictionary::ParsedFileListParser::parsed_files_t * Dictionary::ParsedFileListParser::parse()
{
	XERCES_CPP_NAMESPACE_USE
	try {
        XMLPlatformUtils::Initialize();
       }
       catch (const XMLException& toCatch) {
            char* message = XMLString::transcode(toCatch.getMessage());
						std::cout << "Error during initialization! :\n";
            std::cout << "Exception message is: \n" << message << "\n";
            XMLString::release(&message);
            return NULL;
        }

			  const char* xmlFile = this->filelist_name.c_str();
        SAX2XMLReader* parser = XMLReaderFactory::createXMLReader();
        parser->setFeature(XMLUni::fgSAX2CoreValidation, false);   // optional
        parser->setFeature(XMLUni::fgSAX2CoreNameSpaces, false);   // optional

        parser->setContentHandler(this);
        parser->setErrorHandler(this);

        try {
					  parsed_files = new parsed_files_t();
						is_file_element = false;
            parser->parse(xmlFile);
        }
        catch (const XMLException& toCatch) {
            char* message = XMLString::transcode(toCatch.getMessage());
            std::cout << "Exception message is: \n" << message << "\n";
            XMLString::release(&message);
            return NULL;
        }
        catch (const SAXParseException& toCatch) {
            char* message = XMLString::transcode(toCatch.getMessage());
            std::cout << "Exception message is: \n" << message << "\n";
            XMLString::release(&message);
            return NULL;
        }
				catch(const std::bad_exception &ex) {
					  std::cout << "bad_exception in file " << xmlFile << " : " << ex.what() << std::endl;
            return NULL;
				}
        catch (...) {
            std::cout << "Unexpected Exception in file " << xmlFile << "\n" ;
            return NULL;
        }

        delete parser;
		return parsed_files;
}

void Dictionary::ParsedFileListParser::add_file(char *file)
{
	std::string filename = file;
	parsed_files->push_back(filename);
}